package com.demo.core.util.collection;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.demo.core.model.support.AdjacencyListModel;

/**
 * JSON工具
 * 将模型成成
 * @author ryuu
 *
 */
public abstract class JsonUtils {

	public static String childFormat = "{id:\'%d\', pId:\'%d\', originalId:\'%d\', name:'%s', isParent:false, checked:\'%s\'},";
	public static String parentFormat = "{id:\'%d\', pId:\'%d\', originalId:\'%d\', size:\'%d\', name:'%s', isParent:true, open:%s, checked:\'%s\'},";
	/**
	 * 将模型生成zTree的json字符串
	 * @param root AdjacencyListModel
	 * @return json
	 */
	/*
	 * var zNodes =[
			{ id:1, pId:0, name:"父节点1 - 展开", open:true},
			{ id:11, pId:1, name:"父节点11 - 折叠"},
			{ id:111, pId:11, name:"叶子节点111"},
			{ id:112, pId:11, name:"叶子节点112"},
			{ id:113, pId:11, name:"叶子节点113"},
			{ id:114, pId:11, name:"叶子节点114"},
			{ id:12, pId:1, name:"父节点12 - 折叠"},
			{ id:121, pId:12, name:"叶子节点121"},
			{ id:122, pId:12, name:"叶子节点122"},
			{ id:123, pId:12, name:"叶子节点123"},
			{ id:124, pId:12, name:"叶子节点124"},
			{ id:13, pId:1, name:"父节点13 - 没有子节点", isParent:true},
			{ id:2, pId:0, name:"父节点2 - 折叠"},
			{ id:21, pId:2, name:"父节点21 - 展开", open:true},
			{ id:211, pId:21, name:"叶子节点211"},
			{ id:212, pId:21, name:"叶子节点212"},
			{ id:213, pId:21, name:"叶子节点213"},
			{ id:214, pId:21, name:"叶子节点214"},
			{ id:22, pId:2, name:"父节点22 - 折叠"},
			{ id:221, pId:22, name:"叶子节点221"},
			{ id:222, pId:22, name:"叶子节点222"},
			{ id:223, pId:22, name:"叶子节点223"},
			{ id:224, pId:22, name:"叶子节点224"},
			{ id:23, pId:2, name:"父节点23 - 折叠"},
			{ id:231, pId:23, name:"叶子节点231"},
			{ id:232, pId:23, name:"叶子节点232"},
			{ id:233, pId:23, name:"叶子节点233"},
			{ id:234, pId:23, name:"叶子节点234"},
			{ id:3, pId:0, name:"父节点3 - 没有子节点", isParent:true}
		];
	 */
	/**
	 * 生成zTree树结构数据(JSON)
	 * @param root 节点数
	 * @param isAllOpen 是否允许打开
	 * @param allowKey 允许的节点集合
	 * @param selected 定义被选中的ID集合
	 * @return json字符串
	 */
	public static String zTree(AdjacencyListModel root, boolean isAllOpen, Object... selected) {
		
		StringBuilder json = new StringBuilder();
		Set<Object> keySet = new HashSet<Object>();
		if (selected != null) {
			for (Object id : selected) {
				// 不存在allowKey或者allowKey存在指定id,则加入
				//if (allowKey == null || allowKey.contains(id)) {
				keySet.add(id);
				//}
			}
		}
		json.append("[");
		if (root != null) {
			boolean isOpen = isAllOpen; //|| keySet.contains(root.getId()) ;
			json.append(String.format(parentFormat, root.getId(), root.getPid(), root.getOriginalId(), root.getDepth(), root.getName(), isOpen, keySet == null ? "" : keySet.contains(root.getOriginalId())));
			List<AdjacencyListModel> childList = root.getChilds();
			if (childList != null) {
				for (AdjacencyListModel node : childList) {
					zTree(node, json, keySet, isAllOpen);
				}
			}
		}
		if (json.length() > 1) {
			json.deleteCharAt(json.length() - 1);
		}
		json.append("]");
		return json.toString();
	}
	
	/**
	 * 
	 * @param node
	 * @param buffer
	 * @param keySet
	 * @param isAllOpen
	 */
	private static final void zTree(AdjacencyListModel node, StringBuilder buffer, Set<Object> keySet, boolean isAllOpen) {
		if (node.isLeaf()) {
			buffer.append(String.format(childFormat, node.getId(), node.getPid(), node.getOriginalId(), node.getName(), keySet == null ? "" : keySet.contains(node.getOriginalId())));
		} else {
			boolean isOpen = isAllOpen;// || keySet.contains(node.getId()) ;
			buffer.append(String.format(parentFormat, node.getId(), node.getPid(), node.getOriginalId(), node.getDepth(), node.getName(), isOpen, keySet == null ? "" :keySet.contains(node.getOriginalId())));
			List<AdjacencyListModel> childList = node.getChilds();
			if (childList != null) {
				for (AdjacencyListModel n : childList) {
					zTree(n, buffer, keySet, isAllOpen);
				}
			}
		}
	}
	
	/**
	 * 根据level层级展开tree
	 * @param root
	 * @param level
	 * @return
	 */
	public static final String zTree(AdjacencyListModel root, int level, Object... selected) {
		StringBuilder json = new StringBuilder();
		Set<Object> keySet = new HashSet<Object>();
		if (selected != null) {
			for (Object id : selected) {
				// 不存在allowKey或者allowKey存在指定id,则加入
				//if (allowKey == null || allowKey.contains(id)) {
				keySet.add(id);
				//}
			}
		}
		if (level < 1) {
			level = 1;
		}
		json.append("[");
		if (root != null) {
			json.append(String.format(parentFormat, root.getId(), root.getPid(), root.getOriginalId(), root.getDepth(), root.getName(), level > 0, keySet == null ? "" : keySet.contains(root.getOriginalId())));
			List<AdjacencyListModel> childList = root.getChilds();
			if (childList != null) {
				for (AdjacencyListModel node : childList) {
					zTree(node, json, level, keySet);
				}
			}
		}
		if (json.length() > 1) {
			json.deleteCharAt(json.length() - 1);
		}
		json.append("]");
		return json.toString();
	}
	
	/**
	 * 
	 * @param node
	 * @param buffer
	 * @param level
	 */
	private static final void zTree(AdjacencyListModel node, StringBuilder buffer, int level, Set<Object> keySet) {
		if (node.isLeaf()) {
			buffer.append(String.format(childFormat, node.getId(), node.getPid(), node.getOriginalId(), node.getName(), node.getDepth() < level, keySet == null ? "" : keySet.contains(node.getOriginalId())));
		} else {
			buffer.append(String.format(parentFormat, node.getId(), node.getPid(), node.getOriginalId(), node.getDepth(), node.getName(), node.getDepth() < level, keySet == null ? "" : keySet.contains(node.getOriginalId())));
			List<AdjacencyListModel> childList = node.getChilds();
			if (childList != null) {
				for (AdjacencyListModel n : childList) {
					zTree(n, buffer, level, keySet);
				}
			}
		}
	}
}
